package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.Adapter.CarAdapter;
import com.huawei.hmsdemo10086.Adapter.OrderAdapter;
import com.huawei.hmsdemo10086.POJO.Car;
import com.huawei.hmsdemo10086.POJO.Order;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CarActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private String userId;
    private List<Car> datas;
    private CarAdapter carAdapter;
    private ListView listView;
    private Button submit;
    int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        flag = 0;
        TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        mTitleTextView.setText("购物车");
        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CarActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);
        listView = (ListView)findViewById(R.id.car_list);
        datas = new ArrayList<Car>();
        submit = (Button)findViewById(R.id.submit_car);
        findUserCar();
        while(flag == 0)
        {
            System.out.println(1);
        }
        carAdapter = new CarAdapter(CarActivity.this,datas);
        listView.setAdapter(carAdapter);
        carAdapter.notifyDataSetChanged();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("click", "click");
                turn_to_pay();
            }
        });
    }

    private void findUserCar()
    {

        String url = "http://10.0.2.2:8443/api/findUserCar";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep.toString());

                Type type = new TypeToken<Result<List<Car>>>(){}.getType();
                Result<List<Car>> result = gson.fromJson(rep,type);
                for(int i = 0 ; i < result.getData().size() ; i++)
                {
                    System.out.println(result.getData().get(i).toString());
                }
                datas = result.getData();
                flag = 1 ;
            }
        });
    }

    private void turn_to_pay()
    {
        Log.d("turn", "turn_to_pay: ");
       List<Car> cars = carAdapter.getChoosed_data();
       if(cars.size() < 1)
       {
           Toast.makeText(CarActivity.this, "至少选择一样商品", Toast.LENGTH_LONG).show();
           return;
       }
       Intent intent = new Intent(CarActivity.this,PayActivity.class);
       intent.putExtra("cars",(Serializable)cars);
       startActivity(intent);
    }




}